package com.quan.system.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.bean.PageBean;
import com.quan.common.bean.ResultBean;
import com.quan.common.core.MyBaseController;
import com.quan.system.entity.SystemJob;
import com.quan.system.service.SystemJobService;

/**
 * <p>
 * 系统-定时作业管理 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-30
 */
@RestController
@RequestMapping("/system/job")
public class SystemJobController extends MyBaseController {

	@Autowired
	private SystemJobService service;

	/**
	 * 添加
	 * 
	 * @param entity
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("/add")
	public Object add(@Valid @RequestBody SystemJob entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultBean.warning(bindingResult.getFieldError().getRejectedValue().toString());
		}
		if(this.service.hasExist("job_name", entity.getJobName(), SystemJob.class)) {
			return ResultBean.warning("该值已存在");
		}
		if(null == entity.getJobGroup()) {
			entity.setJobGroup(Scheduler.DEFAULT_GROUP) ;
		}
		boolean save = this.service.startJob(entity);
		if (save) {
			return ResultBean.ok("提交成功");
		} else {
			return ResultBean.failure("提交失败");
		}
	}

	/**
	 * 根据ID删除
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/deleteById/{id}")
	public Object deleteById(@PathVariable String id) {
		if (StringUtils.isNotBlank(id)) {
			boolean remove = this.service.removeById(id);
			if (remove) {
				return ResultBean.ok();
			} else {
				return ResultBean.failure("通过ID删除失败：[ " + id + " ]");
			}
		}
		return ResultBean.failure("请选项需要删除的记录");
	}

	/**
	 * 根据ID批量删除
	 * 
	 * @param idList ["1","2"]
	 * @return
	 */
	@PostMapping("/deleteBatchIds")
	public Object deleteBatchIds(@RequestBody List<String> idList) {
		if (null != idList && !idList.isEmpty()) {
			boolean remove = this.service.removeByIds(idList);
			if (remove) {
				return ResultBean.ok();
			} else {
				return ResultBean.failure("通过ID删除失败：[ " + idList + " ]");
			}
		}
		return ResultBean.failure("请选项需要删除的记录");
	}

	/**
	 * 修改
	 * 
	 * @param entity
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("/update")
	public Object update(@Valid @RequestBody SystemJob entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultBean.failure(bindingResult.getFieldError().getDefaultMessage().toString());
		}
		boolean update = this.service.updateById(entity);
		if (update) {
			return ResultBean.ok("修改成功");
		} else {
			return ResultBean.failure("修改失败");
		}
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{id}")
	public Object get(@PathVariable Long id) {
		SystemJob entity = this.service.getById(id);
		if (null != entity) {
			return ResultBean.ok(entity);
		} else {
			return ResultBean.failure("通过ID查询失败：[ " + id + " ]");
		}
	}

	/**
	 * 查询所有数据，不分页
	 * 
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/list")
	public Object list(SystemJob entity) {
		return ResultBean.ok(this.service.list(new QueryWrapper<SystemJob>(entity).orderByDesc("ctime")));
	}

	/**
	 * 分页查询数据
	 * 
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/page")
	public Object page(SystemJob entity) {
		return ResultBean.ok(this.service.selectPage(new QueryWrapper<SystemJob>(entity).orderByAsc("job_trigger_date"),
				new PageBean(request)));
	}
	
	/**
	 *  暂停任务
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/pauseJob")
	public Object pauseJob(SystemJob entity) {
		this.service.pauseJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
		return ResultBean.ok();
	}
	
	/**
	 * 恢复任务
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/resumeJob")
	public Object resumeJob(SystemJob entity) {
		this.service.resumeJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
		return ResultBean.ok();
	}
	
	/**
	 * 触发任务
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/triggerJob")
	public Object triggerJob(SystemJob entity) {
		this.service.triggerJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
		return ResultBean.ok();
	}
	

}
